package cn.kk.service.impl;

import cn.kk.base.model.BaseServiceImpl;
import cn.kk.mapper.UserInfoMapper;
import cn.kk.model.user.constant.UserStateEnum;
import cn.kk.model.user.entity.UserInfoEntity;
import cn.kk.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Author: kk
 * @Date: 2023/2/7  20:53
 * @Version 1.0
 */
@Slf4j
@Service
public class UserInfoServiceImpl extends BaseServiceImpl<UserInfoMapper, UserInfoEntity> implements UserInfoService {

    /**
     * 验重
     * @param phone 手机号
     * @param email 邮箱
     * @return 是否重复
     */
    @Override
    public boolean checkRepeat(String phone, String email) {
        if (StringUtils.isEmpty(phone) && StringUtils.isEmpty(email)) {
            return false;
        }
        return lambdaQuery()
                .eq(StringUtils.isNotEmpty(phone), UserInfoEntity::getPhone, phone)
                .eq(StringUtils.isNotEmpty(email), UserInfoEntity::getEmail, email)
                .count() > 1;
    }

    /**
     * 根据手机/邮箱查询用户
     * @param account 手机/邮箱
     * @return 用户信息
     */
    @Override
    public UserInfoEntity search(String account) {
        if (StringUtils.isBlank(account)) {
            return null;
        }
        return lambdaQuery()
                .eq(UserInfoEntity::getPhone, account)
                .or(wrapper -> wrapper.eq(UserInfoEntity::getEmail, account))
                .eq(UserInfoEntity::getState, UserStateEnum.ENABLE.getCode())
                .one();
    }

}
